'use strict';

goog.provide('Blockly.Blocks.texts');

goog.require('Blockly.Blocks');


Blockly.Blocks.texts.HUE = "#ffab19";


// Blockly.Blocks['text'] = {
//   /**
//    * Block for text value.
//    * @this Blockly.Block
//    */
//   init: function() {
//     //this.setHelpUrl(Blockly.Msg.TEXT_TEXT_HELPURL);
//     this.setColour(Blockly.Blocks.texts.HUE);
//     this.appendDummyInput()
//         .appendField(this.newQuote_(true))
//         .appendField(new Blockly.FieldTextInput(''), 'TEXT')
//         .appendField(this.newQuote_(false));
//     this.setOutput(true, 'String');
//     this.setTooltip(Blockly.Msg.TEXT_TEXT_TOOLTIP);
//   },
//   /**
//    * Create an image of an open or closed quote.
//    * @param {boolean} open True if open quote, false if closed.
//    * @return {!Blockly.FieldImage} The field image of the quote.
//    * @private
//    */
//   newQuote_: function(open) {
//     if (open == this.RTL) {
//       var file = '';
//     } else {
//       var file = '';
//     }
//     return new Blockly.FieldImage(file, 12, 12, '"');
//   }
// };


Blockly.FieldTextInput.char_validator = function(text) {
  if (text.length > 1){
	  if(text.charAt(0) === "\\"){
		  var charAtOne = text.charAt(1); 
		  if(charAtOne === "0" || 
		     charAtOne === "b" ||
		     charAtOne === "f" || 
		     charAtOne === "n" ||
		     charAtOne === "r" ||
		     charAtOne === "t" ||
		     charAtOne === "\\" ||
		     charAtOne === "'" ){
			 return String(text).substring(0, 2);
		  }else if(charAtOne === "x" && text.charAt(2) === "0" && text.charAt(3) === "B"){
			  return String(text).substring(0, 4);
		  } 
	  }
  }
  return String(text).substring(0, 1);
};

// Blockly.Blocks['text_char'] = {
//   init: function() {
//     this.setColour(Blockly.Blocks.texts.HUE);
//     this.appendDummyInput()
//         .appendField(this.newQuote_(true))
//         .appendField(new Blockly.FieldTextInput('',Blockly.FieldTextInput.char_validator), 'TEXT')
//         .appendField(this.newQuote_(false));
//     this.setOutput(true, 'Number');
//     this.setTooltip(Blockly.Msg.TEXT_CHAR_TOOLTIP);
//   },
//   newQuote_: function(open) {
//     if (open == true) {
//      var file = "";
     
//     } else {
//       var file = "";
//     }
//     return new Blockly.FieldImage(file, 7, 12, '"');
//   }
// };


// Blockly.Blocks['text_join'] = {
//   init: function() {
//     this.setColour(Blockly.Blocks.texts.HUE);
//     this.appendValueInput('A')
//         .setCheck(['String','Number']);
//     this.appendValueInput('B')
//         .setCheck(['String','Number'])
//         .appendField(Blockly.BUDDY_TEXT_JOIN);
//     this.setInputsInline(true);
// 	this.setOutput(true, 'String');
// 	this.setTooltip(Blockly.BUDDY_TOOLTIP_TEXT_JOIN);
//   }
// };

Blockly.Blocks['text_to_number'] = {
  init: function() {
	var TO_INT_FLOAT =
        [[Blockly.BUDDY_TO_INT, 'toInt'],
        [Blockly.BUDDY_TO_FLOAT, 'toFloat']];
    this.setColour(Blockly.Blocks.texts.HUE);
    this.appendValueInput('VAR')
        .setCheck('String')
		.appendField(new Blockly.FieldDropdown(TO_INT_FLOAT), 'TOWHAT');
  this.setOutput(true, 'Number');
	var thisBlock = this;
    this.setTooltip(function() {
      var mode = thisBlock.getFieldValue('TOWHAT');
      var TOOLTIPS = {
        'toInt': Blockly.BUDDY_TOOLTIP_TEXT_TOINT,
        'toFloat': Blockly.BUDDY_TOOLTIP_TEXT_TOFLOAT
      };
      return TOOLTIPS[mode];
    });
  }
};

Blockly.Blocks['ascii_to_char'] = {
  init: function() {
    this.setColour(Blockly.Blocks.texts.HUE);
    this.appendValueInput('VAR')
        .setCheck('Number')
		.appendField(Blockly.BUDDY_TOCHAR);
	this.setOutput(true, 'String');
	this.setTooltip(Blockly.BUDDY_TOOLTIP_TEXT_TOCHAR);
  }
};

Blockly.Blocks['char_to_ascii'] = {
    init: function () {
        this.setColour(Blockly.Blocks.texts.HUE);
        this.appendDummyInput()
            .appendField(Blockly.BUDDY_TOASCII)
            .appendField("'")
            .appendField(new Blockly.FieldTextInput('',Blockly.FieldTextInput.char_validator), 'TEXT')
            .appendField("'");
        this.setOutput(true, 'Number');
        this.setTooltip(Blockly.BUDDY_TOOLTIP_TEXT_TOASCII);
    }
};

Blockly.Blocks['number_to_text'] = {
    init: function () {
      var TO_INT_FLOAT =
        [[Blockly.Msg.MATH_BIN, 'BIN'],
        [Blockly.Msg.MATH_OCT, 'OCT'],
        [Blockly.Msg.MATH_DEC, 'DEC'],
        [Blockly.Msg.MATH_HEX, 'HEX']];
        this.setColour(Blockly.Blocks.texts.HUE);
        this.appendValueInput('VAR')
            .setCheck('Number')
            .appendField(Blockly.BUDDY_TOSTRING)
            .appendField(new Blockly.FieldDropdown(TO_INT_FLOAT), 'TOWHAT');
        this.setOutput(true, 'String');
        this.setTooltip(Blockly.BUDDY_TOOLTIP_TEXT_TOTEXT);
    }
};

Blockly.Blocks['number_to_text_'] = {
    init: function () {
        this.setColour(Blockly.Blocks.texts.HUE);
        this.appendValueInput('VAR')
            .setCheck('Number')
            .appendField(Blockly.BUDDY_TOSTRING);;
        this.setOutput(true, 'String');
        this.setTooltip(Blockly.BUDDY_TOOLTIP_TEXT_TOTEXT);
    }
};

// Blockly.Blocks['text_length']={
// init: function() {
//     this.setColour(Blockly.Blocks.texts.HUE);
// 	this.appendValueInput("VAR")
//         .appendField(Blockly.BUDDY_LENGTH)
//         .setCheck('String');
// 	this.setOutput(true, 'Number');
//   }
// }

Blockly.Blocks['text_char_at']={
init: function() {
    this.setColour(Blockly.Blocks.texts.HUE);
	this.appendValueInput("VAR")
        .setCheck('String');
	this.appendValueInput("AT")
        .appendField(Blockly.Msg.TEXT_CHARAT)
        .setCheck('Number');
	this.appendDummyInput()
		.appendField(Blockly.Msg.TEXT_CHARAT2);
	this.setOutput(true, 'Number');
	this.setInputsInline(true);
  }
}

Blockly.Blocks['text_equals_starts_ends']={
init: function() {
	var TEXT_DOWHAT =
        [[Blockly.BUDDY_EQUALS, 'equals'],
        [Blockly.BUDDY_STARTSWITH, 'startsWith'],
		[Blockly.BUDDY_ENDSWITH, 'endsWith']];
    this.setColour(Blockly.Blocks.texts.HUE);
	this.appendValueInput("STR1")
        .setCheck(['String','Number']);
	this.appendValueInput("STR2")
        .appendField(new Blockly.FieldDropdown(TEXT_DOWHAT), 'DOWHAT')
        .setCheck(['String','Number']);
	this.setOutput(true, [Boolean,'Number']);
	this.setInputsInline(true);
  }
}

Blockly.Blocks['text_compareTo']={
init: function() {
    this.setColour(Blockly.Blocks.texts.HUE);
	this.appendValueInput("STR1")
       .setCheck(['String','Number']);
	this.appendValueInput("STR2")
        .appendField(Blockly.BUDDY_COMPARETO)
       .setCheck(['String','Number']);
	this.setOutput(true, 'Number');
	this.setInputsInline(true);
	this.setTooltip(Blockly.BUDDY_COMPARETO_HELP);
  }
}